<?php

namespace app\api\controller;



use app\api\model\group\GroupCategory;
use app\api\model\group\GroupProjectApply;
use app\common\controller\Api;
use app\api\model\group\GroupOrder as GroupOrderModel;


class Grouporder extends Api
{
    protected $noNeedRight = ['*'];

    //是否已申请抱团
    public function issign()
    {
        $group_id = input('group_id',0);

        $orderinfo = GroupOrderModel::get(['user_id'=>$this->auth->id, 'group_id'=>$group_id]);

        $this->success('',$orderinfo?$orderinfo['order_id']:0);
    }

    //抱团申请
    public function create()
    {
        try {
            $model = new GroupOrderModel();
            $param = input();
            $param['user_id'] = $this->auth->id;
            $res = $model->apply($param);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);

    }

    //修改抱团申请
    public function edit()
    {
        try {
            $model = new GroupOrderModel();
            $param = input();
            $param['user_id'] = $this->auth->id;
            $res = $model->edit($param);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);

    }

    //我的抱团
    public function lists()
    {
        $list = GroupOrderModel::alias('go')
            ->join('group g','go.group_id = g.group_id')
            ->where(['go.user_id'=>$this->auth->id])
            ->field('g.group_id,g.name,g.people_num,g.notice,g.createtime,go.group_status,go.order_id,go.is_evaluate')
            ->order('g.createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);
        foreach ($list as $k=>$v){
            $category_id =  GroupProjectApply::where(['group_id'=>$v['group_id']])->group('category_id')->column('category_id');
            $category_list = GroupCategory::where('id','in',$category_id)->column('name');
            $list[$k]['category_name'] = implode('/',$category_list);

            $list[$k]['signup_num'] =  GroupOrderModel::where(['group_id'=>$v['group_id'],'group_status'=>1])->count();
        }
        $this->success('',$list);

    }

    //详情
    public function orderInfo()
    {
        $order_id = input('order_id',0);

        $info = GroupOrderModel::get($order_id);
        if (!$info)$this->error('报名记录不存在');

        $this->success('',$info);
    }

    //取消申请
    public function cancel()
    {
        $order_id = input('order_id',0);

        $info = GroupOrderModel::get($order_id);
        if (!$info)$this->error('报名记录不存在');
        $info->delete();
        /*if ($info->is_cancel != 0)$this->error('error');
        $info->is_cancel = 1;
        $info->cancel_time = time();
        $info->group_status = 8;
        $info->save();*/
        $this->success('成功');
    }

    //退团
    public function exit()
    {
        $order_id = input('order_id',0);

        $info = GroupOrderModel::get($order_id);
        if (!$info)$this->error('报名记录不存在');
        if ($info->is_exit != 0)$this->error('error');
        $info->is_exit = 1;
        $info->exit_time = time();
        $info->group_status = 4;
        $info->save();
        $this->success('成功');
    }

    //暂离
    public function leave()
    {
        $order_id = input('order_id',0);

        $info = GroupOrderModel::get($order_id);
        if (!$info)$this->error('报名记录不存在');
        if ($info->is_leave != 0)$this->error('error');
        $info->is_leave = 1;
        $info->leave_time = time();
        $info->group_status = 9;
        $info->save();
        $this->success('成功');
    }


}